package com.example.demo.mapper;

import com.example.demo.entity.AfshopSearchHistory;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 搜索历史表 Mapper 接口
 * </p>
 *
 * @author Mr.We
 * @since 2024-06-26
 */
@Mapper
public interface AfshopSearchHistoryMapper extends BaseMapper<AfshopSearchHistory> {
    /**
     * 根据ID查询搜索历史对象
     *
     * @param id 搜索历史的唯一标识ID
     * @return 返回匹配ID的AfshopSearchHistory对象，如果找不到，则返回null
     */
    @Select("select * from afshop_search_history where id = #{value}")
    AfshopSearchHistory queryObject(Integer id);

    /**
     * 根据条件查询搜索历史列表
     *
     * @param map 包含查询条件的Map对象，键值对形式指定查询参数
     * @return 返回符合条件的搜索历史列表，如果无匹配项，则返回空列表
     */
    @Select("<script>" +
            "select" +
            "        <if test='fields != null and fields != \"\"'>" +
            "            ${fields}" +
            "        </if>" +
            "        <if test='fields == null or fields == \"\"'>" +
            "            *" +
            "        </if>" +
            "        from afshop_search_history" +
            "        <where>" +
            "            <if test='userId != null'>" +
            "                and user_id = #{userId}" +
            "            </if>" +
            "        </where>" +
            "        <choose>" +
            "            <when test='sidx != null and sidx.trim() != \"\"'>" +
            "                order by ${sidx} ${order}" +
            "            </when>" +
            "            <otherwise>" +
            "                order by id desc" +
            "            </otherwise>" +
            "        </choose>" +
            "        <if test='offset != null and limit != null'>" +
            "            limit #{offset}, #{limit}" +
            "        </if>" +
            "</script>")
    List<AfshopSearchHistory> queryList(Map<String, Object> map);

    /**
     * 根据条件查询搜索历史的总数
     *
     * @param map 包含查询条件的Map对象，用于统计满足条件的搜索历史总数
     * @return 返回满足条件的搜索历史总数
     */
    @Select("select count(*) from afshop_search_history")
    int queryTotal(Map<String, Object> map);
    @Delete("delete from nideshop_search_history where  user_id = #{userId}")
    void deleteByUserId(Long userId);
}
